package cn.tiancaifan.micro.service;

import cn.tiancaifan.micro.entity.ConstantProperties;
import cn.tiancaifan.micro.entity.Hr;
import cn.tiancaifan.micro.entity.SystemResults;
import cn.tiancaifan.micro.mapper.HrMapper;
import cn.tiancaifan.micro.utils.AliyunOSSUtil;
import cn.tiancaifan.micro.utils.DataBaseUtils;
import cn.tiancaifan.micro.utils.HrUtils;
import cn.tiancaifan.micro.utils.Logger;
import com.aliyun.oss.model.OSSObjectSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

@Service
public class DataBaseService {

    @Autowired
    HrMapper hrMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    DataBaseUtils dataBaseUtils;
    @Autowired
    AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    ConstantProperties constantProperties;


//    备份数据库完成
    @Logger("备份数据库")
    public SystemResults backUp() {
        String fileName = "mirco"+new Date().getTime()+".sql";
        try {
            Process process = Runtime.getRuntime().exec("mysqldump -uroot -p123456 vhr");
            String upload = aliyunOSSUtil.upload(constantProperties.getSql()+fileName, process.getInputStream());
            return SystemResults.ok("备份成功");
        } catch (IOException e) {
            e.printStackTrace();
            return SystemResults.ok("备份失败");
        }
    }

    public SystemResults recovery(InputStream inputStream) {
        SystemResults systemResults = dataBaseUtils.runSqlJs(new InputStreamReader(inputStream));
        if (systemResults.getCode() == 0){
            return SystemResults.ok("恢复成功！");
        }else {
            return SystemResults.error(systemResults.getMsg());
        }
    }

    @Logger("初始化数据库")
    public SystemResults initSystem(String password) {
        String username = HrUtils.getCurrentHr().getUsername();
        Hr hr = hrMapper.loadUserByUsername(username);
        BCryptPasswordEncoder bCryptPasswordEncoder = (BCryptPasswordEncoder) passwordEncoder;
        boolean matches = bCryptPasswordEncoder.matches(password, hr.getPassword());
        if (matches){
            try {
                InputStream stream = aliyunOSSUtil.exportOssFile(constantProperties.getFilehost() + constantProperties.getInitsql());
                dataBaseUtils.runSqlJs(new InputStreamReader(stream));
                return SystemResults.ok("初始化成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
           return SystemResults.ok("密码错误！");
        }
        return null;
    }

    public SystemResults getListSql() {
        List<OSSObjectSummary> ossObjectSummaries = aliyunOSSUtil.getObjectList(constantProperties.getFilehost() + constantProperties.getSql(), "fanxiaofan");
        ossObjectSummaries.remove(0);
        return SystemResults.ok(null,ossObjectSummaries);
    }

    public SystemResults deleteSql(OSSObjectSummary key) {
        String s = aliyunOSSUtil.deleteBlog(key.getKey());
        return SystemResults.ok("删除成功");
    }

    public SystemResults recoverySql(OSSObjectSummary ossObjectSummary) {
        try {
            InputStream inputStream = aliyunOSSUtil.exportOssFile(ossObjectSummary.getKey());
            SystemResults systemResults = dataBaseUtils.runSqlJs(new InputStreamReader(inputStream));
            return systemResults;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return SystemResults.error("恢复失败");
    }
}
